<?php


class Order extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Order the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'order';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            array('quantity','numerical'),
			array('price, money, origin, fare', 'numerical'),
			array('pay_id, realname, express_no', 'length', 'max'=>32),
			array('service', 'length', 'max'=>16),
			array('user_id, admin_id, team_id, city_id, quantity, express_id, create_time, pay_time', 'length', 'max'=>10),
			array('state', 'length', 'max'=>5),
			array('mobile, address, express_xx', 'length', 'max'=>256),
			array('zipcode', 'length', 'max'=>6),
			array('express', 'length', 'max'=>1),
			array('remark', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, pay_id, service, user_id, admin_id, team_id, city_id, state, quantity, realname, mobile, zipcode, address, express, express_xx, express_id, express_no, price, money, origin, fare, remark, create_time, pay_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		 'team'=>array(self::BELONGS_TO,'Team','team_id'),


		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(

			'id' => 'ID',
			'pay_id' => '订单ID',
			'service' => '支付方式',
			'user_id' => '客户ID',
			'admin_id' => '管理员ID',
			'team_id' => '团购ID',
			'city_id' => '城市',
			'state' => '订单状态',
			'quantity' => '数量',
			'realname' => '用户姓名',
			'mobile' => '手机号码',
			'zipcode' => '邮编',
			'address' => '详细地址',
			'express' => '快递',
			'express_xx' => '快递公司名',
			'express_id' => '快递公司ID',
			'express_no' => '快递NO',
			'price' => '单价',
			'money' => '账户余额',
			'origin' => '总价',
			'fare' => '快递费用',
			'remark' => '订单附言',
			'create_time' => '创建时间',
			'pay_time' => '支付时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */

	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('pay_id',$this->pay_id,true);
		$criteria->compare('service',$this->service,true);
		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('admin_id',$this->admin_id,true);
		$criteria->compare('team_id',$this->team_id,true);
		$criteria->compare('city_id',$this->city_id,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('quantity',$this->quantity,true);
		$criteria->compare('realname',$this->realname,true);
		$criteria->compare('mobile',$this->mobile,true);
		$criteria->compare('zipcode',$this->zipcode,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('express',$this->express,true);
		$criteria->compare('express_xx',$this->express_xx,true);
		$criteria->compare('express_id',$this->express_id,true);
		$criteria->compare('express_no',$this->express_no,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('money',$this->money);
		$criteria->compare('origin',$this->origin);
		$criteria->compare('fare',$this->fare);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('pay_time',$this->pay_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	  protected function beforeSave()
{
    if(parent::beforeSave())
    {
        if($this->isNewRecord)
        {
            $this->create_time=time();

        }

        return true;
    }
 }

}